package com.wgj.android.dialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XLoadingDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.accesslibility.config.BatchGroupConfig;
import com.wgj.android.accesslibility.config.WXConfginUtil;
import com.wgj.android.accesslibility.service.WGJAccessServier;
import com.wgj.android.permission.WindowManagerPermission;
import com.wgj.android.utils.AccessibilityUtil;
import com.wgj.android.utils.WXAccessUtil;

import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.OnTouch;
/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/12/19
 * 描    述：
 * 修订历吏：
 * =========================================
 */
public class BAFriendsFormGroupDialog extends AppCompatDialogFragment {
    private Dialog dialog;
    private View view;
    @BindView(R.id.vMessage)
    EditText vMessage;
    @BindView(R.id.vNumber)
    EditText vNumber;
    @BindDrawable(R.mipmap.icon_save)
    Drawable saveDrawable;
    private BatchGroupConfig config;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        config= WXConfginUtil.getBatchGroupConfig(getContext());

        super.onCreate(savedInstanceState);
        if (view == null){
            super.onCreate(savedInstanceState);
            view = LayoutInflater.from(getActivity()).inflate(R.layout.b_friends_group_dialog, null, false);
        }
        ButterKnife.bind(this,view);
        saveDrawable.setBounds(0, 0, saveDrawable.getMinimumWidth(), saveDrawable.getMinimumHeight());
        loadData();
    }
    public  void  loadData(){
        vMessage.setText(config.getMessage());
        vNumber.setText(config.getFilterNum()+"");
    }

    @OnTextChanged(value = R.id.vMessage,callback = OnTextChanged.Callback.TEXT_CHANGED)
    public  void  onMessageChanged(CharSequence s, int start, int before, int count){
        if(s.length()==0){
             vMessage.setCompoundDrawables(null,null,null,null);
             return;
        }
        if(s.toString().equals(config.getMessage())){
            vMessage.setCompoundDrawables(null,null,null,null);
        }else {
            vMessage.setCompoundDrawables(null,null,saveDrawable,null);
        }
    }
    @OnTextChanged(value = R.id.vNumber,callback = OnTextChanged.Callback.TEXT_CHANGED)
    public  void  onNumberChanged(CharSequence s, int start, int before, int count){
        if(s.length()==0){
            vNumber.setCompoundDrawables(null,null,null,null);
            return;
        }
        if(s.toString().equals(String.valueOf(config.getFilterNum()))){
            vNumber.setCompoundDrawables(null,null,null,null);
        }else {
            vNumber.setCompoundDrawables(null,null,saveDrawable,null);
        }

    }


    @OnTouch(R.id.vMessage)
    public boolean onMessageTouch(View view,MotionEvent event){
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                Drawable drawableRight =vMessage.getCompoundDrawables()[2] ;
                if (drawableRight != null && event.getRawX() >= (vMessage.getRight() - drawableRight.getBounds().width())) {
                    config.setMessage(vMessage.getText().toString());
                    WXConfginUtil.saveBatchGroupConfig(getContext(),config);
                    Toast.makeText(getContext(),"保存成功",Toast.LENGTH_SHORT).show();
                    vMessage.setCompoundDrawables(null,null,null,null);
                    return false ;
                }
                break;
        }
        return false;
    }
    @OnTouch(R.id.vNumber)
    public boolean onNumberTouch(View view,MotionEvent event){
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                Drawable drawableRight =vNumber.getCompoundDrawables()[2] ;
                if (drawableRight != null && event.getRawX() >= (vNumber.getRight() - drawableRight.getBounds().width())) {
                    config.setFilterNum(Integer.valueOf(vNumber.getText().toString()));
                    WXConfginUtil.saveBatchGroupConfig(getContext(),config);
                    Toast.makeText(getContext(),"保存成功",Toast.LENGTH_SHORT).show();
                    vNumber.setCompoundDrawables(null,null,null,null);
                    return true ;
                }
                break;
        }
        return false;
    }
    @OnClick(R.id.closeBut)
    public  void  onCloseClick(View view){
        dismiss();
    }

    @OnClick(R.id.startTask)
    public  void  onStartTask(View view){
        WXConfginUtil.closeAddFriendByLocation();
        WXConfginUtil.openAddFriendsByGropu();
        ComponentName comp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
        Intent it = new Intent();
        it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//需要加这个不然会报错
        it.setComponent(comp);
        startActivity(it);
        dismiss();


    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);
        if(dialog==null){
            // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
            initDialogStyle(view);
        }
        return dialog;
    }

    private void initDialogStyle(View view) {
        dialog = new Dialog(getActivity(), R.style.dialog_setting);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.popupwindow_anim_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width =WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }
}
